package work.thread_eg.pool;

import cn.hutool.core.date.DateUtil;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 *
 * 单线程的线程池
 *
 * 特点
 *      1 和FixedThreadPool类似, 但是核心线程和最大线程数都是1, 永远只有一个线程(不断复用)
 *
 *
 */
public class SingleThreadExecutorMain {

    public static void main(String[] args) {

        ExecutorService executorService = Executors.newSingleThreadExecutor();
        for (int i = 0; i < 10; i++) {
            executorService.execute(() -> {
                // 获取线程名称,默认格式:pool-1-thread-1
                System.out.println(DateUtil.now() + " " + Thread.currentThread().getName() + " ");
                // 等待2秒
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException exception) {
                    exception.printStackTrace();
                }
            });

        }
    }
}
